package com.alianzamedica.view;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.objectsearch.sqlsearch.ObjectSearch;
import org.objectsearch.web.tools.Converter;
import org.w3c.dom.Document;

import com.alianzamedica.businessobject.Doctor;
import com.alianzamedica.businessobject.Profile;
import com.alianzamedica.tools.Enviroment;
import com.alianzamedica.tools.ValidateTool;

/**
 * @author Carlos
 * 
 */
public class MedicEditForm extends ActionForm {

	/**
	 * 
	 */
	private static final long serialVersionUID = -5932912785683046501L;

	private String id;
	private String firstName;
	private String lastName;
	private String email;
	private String password;
	private String confirmPassword;
	private String university;
	private String speciality;
	private String cell;
	private String stateCell;
	private String certificationNO;
	private String phoneNo;
	private String location;
	private String radio;
	private String hour;
	private String profile;

	/**
	 * id.
	 * 
	 * @return regresa id.
	 */
	public String getId() {
		return id;
	}

	/**
	 * @param id
	 */
	public void setId(String id) {
		this.id = id;
	}

	/**
	 * regresa nombre.
	 * 
	 * @return nombre.
	 */
	public String getFirstName() {
		return firstName;
	}

	/**
	 * apellido.
	 * 
	 * @param firstName
	 *            apellido.
	 */
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	/**
	 * apellido.
	 * 
	 * @return apellido.
	 */
	public String getLastName() {
		return lastName;
	}

	/**
	 * apellido.
	 * 
	 * @param lastName
	 *            apellido.
	 */
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	/**
	 * email.
	 * 
	 * @return email.
	 */
	public String getEmail() {
		return email;
	}

	/**
	 * @param email
	 */
	public void setEmail(String email) {
		this.email = email;
	}

	/**
	 * contraseña.
	 * 
	 * @return contraseña.
	 */
	public String getPassword() {
		return password;
	}

	/**
	 * contraseña.
	 * 
	 * @param password
	 *            contraseña.
	 */
	public void setPassword(String password) {
		this.password = password;
	}

	/**
	 * confirmar contraseña.
	 * 
	 * @return confirmar contraseña.
	 */
	public String getConfirmPassword() {
		return confirmPassword;
	}

	/**
	 * confirmar contraseña.
	 * 
	 * @param confirmPassword
	 *            confirmar contraseña.
	 */
	public void setConfirmPassword(String confirmPassword) {
		this.confirmPassword = confirmPassword;
	}

	/**
	 * universidad.
	 * 
	 * @return universidad.
	 */
	public String getUniversity() {
		return university;
	}

	/**
	 * universidad.
	 * 
	 * @param university
	 *            universidad.
	 */
	public void setUniversity(String university) {
		this.university = university;
	}

	/**
	 * especialidad.
	 * 
	 * @return especialidad.
	 */
	public String getSpeciality() {
		return speciality;
	}

	/**
	 * especialidad.
	 * 
	 * @param speciality
	 *            especialidad.
	 */
	public void setSpeciality(String speciality) {
		this.speciality = speciality;
	}

	/**
	 * celular.
	 * 
	 * @return celular.
	 */
	public String getCell() {
		return cell;
	}

	/**
	 * cedula.
	 * 
	 * @param cell
	 *            cedula.
	 */
	public void setCell(String cell) {
		this.cell = cell;
	}

	/**
	 * cedula estatal.
	 * 
	 * @return cedula estatal.
	 */
	public String getStateCell() {
		return stateCell;
	}

	/**
	 * cedula estatal.
	 * 
	 * @param stateCell
	 *            cedula estatal.
	 */
	public void setStateCell(String stateCell) {
		this.stateCell = stateCell;
	}

	/**
	 * numero de certificacion.
	 * 
	 * @return numero de certificacion.
	 */
	public String getCertificationNO() {
		return certificationNO;
	}

	/**
	 * numero de certificacion.
	 * 
	 * @param certificationNO
	 *            numero de certificacion.
	 */
	public void setCertificationNO(String certificationNO) {
		this.certificationNO = certificationNO;
	}

	/**
	 * numero de telefono.
	 * 
	 * @return numero de telefono.
	 */
	public String getPhoneNo() {
		return phoneNo;
	}

	/**
	 * numero de telefono.
	 * 
	 * @param phoneNo
	 *            numero de telefono.
	 */
	public void setPhoneNo(String phoneNo) {
		this.phoneNo = phoneNo;
	}

	/**
	 * direccion.
	 * 
	 * @return direccion.
	 */
	public String getLocation() {
		return location;
	}

	/**
	 * localización.
	 * 
	 * @param location
	 *            localización.
	 */
	public void setLocation(String location) {
		this.location = location;
	}

	/**
	 * radio.
	 * 
	 * @return radio.
	 */
	public String getRadio() {
		return radio;
	}

	/**
	 * radio.
	 * 
	 * @param radio
	 *            radio.
	 */
	public void setRadio(String radio) {
		this.radio = radio;
	}

	/**
	 * hora.
	 * 
	 * @return hora.
	 */
	public String getHour() {
		return hour;
	}

	/**
	 * hora.
	 * 
	 * @param hour
	 *            hora.
	 */
	public void setHour(String hour) {
		this.hour = hour;
	}

	/**
	 * perfil.
	 * 
	 * @return perfil.
	 */
	public String getProfile() {
		return profile;
	}

	/**
	 * perfil.
	 * 
	 * @param profile
	 *            perfil.
	 */
	public void setProfile(String profile) {
		this.profile = profile;
	}

	/**
	 * regresa el doctor contenido en la forma.
	 * 
	 * @return doctor contenido en la forma.
	 */
	public Doctor getDoctor() {
		Doctor doctor = new Doctor();
		doctor.setId(Converter.string2Integer(id));
		doctor.setFirstName(firstName);
		doctor.setLastName(lastName);
		doctor.setEmail(email);
		doctor.setPassword(password);
		doctor.setUniversity(university);
		doctor.setSpeciality(speciality);
		doctor.setCell(cell);
		doctor.setStateCell(stateCell);
		doctor.setCertificationNO(certificationNO);
		doctor.setPhoneNo(phoneNo);
		doctor.setLocation(location);
		doctor.setRadio(radio);
		doctor.setHour(hour);
		doctor.setProfile(Converter.string2Integer(profile));
		return doctor;
	}

	@SuppressWarnings("unchecked")
	@Override
	public ActionErrors validate(ActionMapping mapping,
			HttpServletRequest request) {
		ActionErrors errors = new ActionErrors();
		ValidateTool validateTool = new ValidateTool();

		if (validateTool.isStringEmpty(firstName)) {
			errors.add("firstName", new ActionMessage("Nombre es requerido"));
		}
		if (validateTool.isStringEmpty(lastName)) {
			errors.add("lastName", new ActionMessage("Apellido es requerido"));
		}
		if (validateTool.isStringEmpty(email)) {
			errors.add("email", new ActionMessage("Email es requerido"));
		} else if (!validateTool.isEmail(email)) {
			errors.add("email", new ActionMessage(
					"El Email no tiene formato v&aacute;lido"));
		}
		if (validateTool.isStringEmpty(password)) {
			errors.add("password", new ActionMessage(
					"Contrase&ntilde;a es requerido"));
		}
		if (!confirmPassword.equals(password)) {
			errors.add("confirmpassword", new ActionMessage(
					"Las contrase&ntilde;as no conciden"));
		}

		if (errors.size() > 0) {
			request.setAttribute("error", errors);
			Doctor medic = this.getDoctor();
			request.setAttribute("medic", medic);

			Enviroment env = Enviroment.getInstance();
			Document doc = env.getDocument();
			ObjectSearch search;
			try {
				search = new ObjectSearch(doc,
						"com.alianzamedica.connection.ConnectionImpl");
				Profile p = new Profile();
				ArrayList list = search.searchObjects(p);
				request.setAttribute("profiles", list);
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			} catch (InstantiationException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		return errors;
	}

}
